<?php

namespace iBrand\Shop\Core\Models;

use iBrand\Component\Balance\Model\BalanceOrder as BaseBalanceOrder;
use iBrand\Component\Recharge\Models\RechargeRule;
use iBrand\Component\Payment\Models\Payment;

class BalanceOrder extends BaseBalanceOrder
{
	protected $table = 'el_balance_order';

	protected $guarded = ['id'];

	protected $appends = ['balance_title', 'amount_yuan', 'pay_amount_yuan', 'channel_text', 'date'];

	public function clerkBalance()
	{
		return $this->hasOne('\iBrand\Shop\Core\Models\ClerkBalanceOrder', 'balance_order_id', 'id');
	}

	public function rule()
	{
		return $this->belongsTo(RechargeRule::class, 'recharge_rule_id', 'id');
	}

	public function user()
	{
		return $this->hasOne(User::class, 'id', 'user_id');
	}

	public function payments()
	{
		return $this->hasMany(Payment::class, 'order_id');
	}

	public function getAmountYuanAttribute()
	{
		return $this->amount / 100;
	}

	public function getPayAmountYuanAttribute()
	{
		return $this->pay_amount / 100;
	}

	public function getBalanceTitleAttribute()
	{
		return '充' . ($this->pay_amount / 100) . '送' . ($this->amount - $this->pay_amount) / 100;
	}

	public function getDateAttribute()
	{
		return date('Y年m月', strtotime($this->created_at));
	}

	public function getImagesAttribute($value)
	{
		if (empty($value)) {
			return [];
		}

		return json_decode($value);
	}

	public function getChannelTextAttribute()
	{
		switch ($this->pay_type) {
			case 'test':
				return "测试";
				break;
			case 'wx_pub_qr':
				return "微信扫码";
				break;

			case 'ali_scan_pay':
				return '支付宝扫码';
				break;

			case 'pop_cash_pay':
				return "刷卡";
				break;

			case 'cash_pay':
				return '现金';
				break;

			default:
				return '';
		}
	}

}